In [1]:
%reload_ext autoreload
%autoreload 2

import numpy as np

import sys
sys.path.append('..')

from helper import nn
from helper import logistic_regression as lr

from sklearn.metrics import classification_report

model

load weights and data


In [2]:
theta1, theta2 = nn.load_weight('ex3weights.mat')

theta1.shape, theta2.shape


Out[2]:
((25, 401), (10, 26))

The original data is 90 degree off. So in data loading function, I use transpose to fix it.
However, the transposed data is not compatible to given parameters because those parameters are trained by original data. So for the sake of applying given parameters, I need to use original data


In [3]:
X, y = nn.load_data('ex3data1.mat',transpose=False)

X = np.insert(X, 0, values=np.ones(X.shape[0]), axis=1)  # intercept

X.shape, y.shape


Out[3]:
((5000, 401), (5000,))

feed forward prediction


In [4]:
a1 = X

In [5]:
z2 = a1 @ theta1.T # (5000, 401) @ (25,401).T = (5000, 25)
z2.shape


Out[5]:
(5000, 25)

In [6]:
z2 = np.insert(z2, 0, values=np.ones(z2.shape[0]), axis=1)

In [7]:
a2 = lr.sigmoid(z2)
a2.shape


Out[7]:
(5000, 26)

In [8]:
z3 = a2 @ theta2.T
z3.shape


Out[8]:
(5000, 10)

In [9]:
a3 = lr.sigmoid(z3)
a3


Out[9]:
array([[  1.38245045e-04,   2.05540079e-03,   3.04012453e-03, ...,
          4.91017499e-04,   7.74325818e-03,   9.96229459e-01],
       [  5.87756717e-04,   2.85026516e-03,   4.14687943e-03, ...,
          2.92311247e-03,   2.35616705e-03,   9.96196668e-01],
       [  1.08683616e-04,   3.82659802e-03,   3.05855129e-02, ...,
          7.51453949e-02,   6.57039547e-03,   9.35862781e-01],
       ..., 
       [  6.27824726e-02,   4.50406476e-03,   3.54510925e-02, ...,
          2.63669734e-03,   6.89448164e-01,   2.74369466e-05],
       [  1.01908736e-03,   7.34360211e-04,   3.78558700e-04, ...,
          1.45616578e-02,   9.75989758e-01,   2.33374461e-04],
       [  5.90807037e-05,   5.41717668e-04,   2.58968308e-05, ...,
          7.00508308e-03,   7.32814653e-01,   9.16696059e-02]])

In [10]:
y_pred = np.argmax(a3, axis=1) + 1  # numpy is 0 base index, +1 for matlab convention
y_pred.shape


Out[10]:
(5000,)

accuracy

so... accuracy on training data is not predicting the real world performance you know
All we can say is NN is very powerful model. Overfitting is easy here.


In [11]:
print(classification_report(y_pred, y_pred))


             precision    recall  f1-score   support

          1       1.00      1.00      1.00       508
          2       1.00      1.00      1.00       493
          3       1.00      1.00      1.00       491
          4       1.00      1.00      1.00       499
          5       1.00      1.00      1.00       503
          6       1.00      1.00      1.00       507
          7       1.00      1.00      1.00       495
          8       1.00      1.00      1.00       502
          9       1.00      1.00      1.00       497
         10       1.00      1.00      1.00       505

avg / total       1.00      1.00      1.00      5000


In [ ]: